package javaer.starter.model;


import javaer.starter.idea.util.sql.SQLEscape;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;

/**
 * @author yangzhen
 * @date 2023/7/13 15:34
 * @since 1.0.0
 */
public interface IModel extends Serializable {

    List<Object> getSqlValues();

    default String getSql() {
        return joinValues(getSqlValues());
    }

    default String joinValues(List<Object> params) {
        StringBuilder builder = new StringBuilder();
        builder.append("(");
        for (Object p : params) {
            if (Objects.nonNull(p)) {
                p = SQLEscape.escape(p.toString());
            }
            builder.append(p).append(",");
        }
        builder.deleteCharAt(builder.lastIndexOf(","));
        builder.append(")");
        return builder.toString();
    }
}
